package cn.jltx.abslistviewadapterdemo.ExpandableBase;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cn.jltx.abslistviewadapterdemo.R;
import cn.jltx.abslistviewadapterdemo.Utils;

/**
 * @author jltxseo
 *         Created by junlintianxia on 2016/05/03.
 */

public class ExpandableViewActivity extends AppCompatActivity implements View.OnClickListener{
    private Button bottomBtn;
    private ExpandableListView expandableListView;
    private List<GroupBean> groupBeanList;
    private ExpandableAdapter expandableAdapter;
    private int lastExpand = -1;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.expandable_list_layout);
        initData();
        assign();
        bottomBtn.setOnClickListener(this);
        expandableAdapter = new ExpandableAdapter(this,groupBeanList);
        expandableListView.setAdapter(expandableAdapter);
        expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {
                Toast.makeText(ExpandableViewActivity.this, "展开第"+groupPosition+"个分组", Toast.LENGTH_SHORT).show();
                int lastIndex = lastExpand;
                lastExpand = groupPosition;
                if(lastIndex != -1 && lastIndex != groupPosition){
                    expandableListView.collapseGroup(lastIndex);
                }
            }
        });
        expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
            @Override
            public void onGroupCollapse(int groupPosition) {
                if(lastExpand != -1 && lastExpand == groupPosition){
                    lastExpand = -1;
                }
                Toast.makeText(ExpandableViewActivity.this, "收起第"+groupPosition+"个分组", Toast.LENGTH_SHORT).show();
            }
        });
        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                Toast.makeText(ExpandableViewActivity.this, "点击第"+groupPosition+"个分组"+"里面的第"+childPosition+"个子元素", Toast.LENGTH_SHORT).show();
                return false;
            }
        });
        expandableListView.expandGroup(0);
    }

    public void initData(){
        if(groupBeanList == null){
            groupBeanList = new ArrayList<GroupBean>();
        }
        GroupBean groupBean;
        ItemBean itemBean;
        for(int i=0; i < 14; i++){
            groupBean = new GroupBean("parent"+(i+1));
            int id = Utils.getResourcesIdFromName(this,"drawable","iv_lol_icon"+(i+1));
            for (int j= 0; j < 5; j++){
                itemBean = new ItemBean(id,"child-"+(j+1));
                groupBean.addChild(itemBean);
            }
            groupBeanList.add(groupBean);
        }


    }
    private void assign(){
        bottomBtn = (Button)this.findViewById(R.id.bottom_btn);
        expandableListView = (ExpandableListView)this.findViewById(R.id.myExpandableView);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bottom_btn:
                finish();
                break;
            default:
                break;
        }
    }
}
